<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Axios 二次封装</title>
    <link crossorigin="anonymous" href="https://cdn.bootcss.com/nprogress/0.2.0/nprogress.css" rel="stylesheet">
    <script crossorigin="anonymous" src="https://cdn.bootcss.com/nprogress/0.2.0/nprogress.js"></script>
    <script crossorigin="anonymous" src="https://cdn.bootcss.com/axios/0.19.2/axios.min.js"></script>
</head>

<body>

    <div>
        <button onclick="send()">获取用户列表</button>
        <button onclick="getRepos()">获取仓库列表</button>
    </div>
    <script>
        axios.interceptors.request.use(config => {
            config.baseURL = 'http://localhost:8000';
            config.headers.token = 'atguigui';
            config.timeout = 1000;
            // 开启loading
            NProgress.start();
            return config;
        }, error => {
            return Promise.reject(error);
        });

        axios.interceptors.response.use(response => {
            NProgress.done();
            return response.data;
        }, error => {
            // 请求失败进行统一处理，不让每个请求单独处理了。
            // 失败就在此处处理，不在将失败向下传递。就需要返回一个 pending状态的promise
            console.log('恭喜你失败了，你有了妈妈');
            NProgress.done();
            // return Promise.reject(error);
            return new Promise(()=>{})
        });

        function send() {
            axios({
                method: 'get',
                url: '/timeout'
            }).then(response => {
                console.log('response', response);
            }).catch(e => {
                console.log('e', e);
            })
        }


    </script>

</body>

</html>